package com.file.manage.domain.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.file.manage.domain.data.OperationLogsVO;
import com.file.manage.domain.entity.File;
import com.file.manage.domain.entity.OperationLogs;
import com.file.manage.domain.entity.User;
import com.file.manage.domain.entity.convertor.OperationLogsConvertor;
import com.file.manage.domain.enums.OperationType;
import com.file.manage.domain.param.OperationLogsQueryParam;
import com.file.manage.domain.service.OperationLogsService;
import com.file.manage.infrastructure.base.Pager;
import com.file.manage.infrastructure.base.dto.query.PageQuery;
import com.file.manage.infrastructure.base.service.impl.BaseServiceImpl;
import com.file.manage.infrastructure.config.CurrentUserCache;
import com.file.manage.infrastructure.dao.UserDao;
import com.file.manage.infrastructure.dao.impl.OperationLogsDaoImpl;
import com.file.manage.infrastructure.utils.ConvertUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author H41
 * @Type OperationLogsServiceImpl.java
 * @date 2025/5/31 12:31
 * @Desc
 */
@Service
public class OperationLogsServiceImpl extends BaseServiceImpl<OperationLogsDaoImpl, OperationLogs> implements OperationLogsService {

    @Resource
    private UserDao userDao;

    @Override
    public Pager<OperationLogsVO> pageQuery(PageQuery<OperationLogsQueryParam> pageQuery) {
        OperationLogsQueryParam entity = pageQuery.getEntity();
        if (StrUtil.isNotBlank(entity.getOperationUserName())) {
            List<User> users = userDao.selectUserByName(entity.getOperationUserName());
            if (CollUtil.isNotEmpty(users)) {
                entity.setUserIds(ConvertUtil.getIds(users));
            }
        }
        List<OperationLogsVO> vos = new ArrayList<>();

        Page<OperationLogs> operationLogsPage = this.baseDao.pageQuery(new Page<>(pageQuery.getCurrent(), pageQuery.getLimit()), entity);
        List<String> userIds = new ArrayList<>();
        List<OperationLogs> records = operationLogsPage.getRecords();

        if (CollUtil.isNotEmpty(records)) {
            records.forEach(record -> {
                userIds.add(record.getOperationUser());
                userIds.add(record.getCreatedBy());
                userIds.add(record.getUpdatedBy());
            });
            Map<String, User> userMap = userDao.mapByIds(userIds);
            records.forEach(record -> {
                OperationLogsVO vo = OperationLogsConvertor.INSTANCE.toFileVO(record,
                        userMap.get(record.getCreatedBy()) == null ? null : userMap.get(record.getCreatedBy()).getName(),
                        userMap.get(record.getUpdatedBy()) == null ? null : userMap.get(record.getUpdatedBy()).getName(),
                        userMap.get(record.getOperationUser()) == null ? null : userMap.get(record.getOperationUser()).getName());
                vos.add(vo);
            });
        }
        return new Pager<>(operationLogsPage.getTotal(), vos);
    }

    @Override
    public void recordingCreateFolder(String filePath) {
        OperationLogs operationLogs = new OperationLogs();
        operationLogs.setDes(CurrentUserCache.get().getName() + "新建文件夹：" + filePath);
        operationLogs.setType(OperationType.CREATE_FOLDER);
        operationLogs.setOperationUser(CurrentUserCache.getId());
        this.save(operationLogs);
    }

    @Override
    public void recordingUpload(String filePath) {
        OperationLogs operationLogs = new OperationLogs();
        operationLogs.setDes(CurrentUserCache.get().getName() + "上传了" + filePath);
        operationLogs.setType(OperationType.UPLOAD);
        operationLogs.setOperationUser(CurrentUserCache.getId());
        this.save(operationLogs);

    }

    @Override
    public void recordingDelete(String filePath) {
        OperationLogs operationLogs = new OperationLogs();
        operationLogs.setDes(CurrentUserCache.get().getName() + "删除了" + filePath);
        operationLogs.setType(OperationType.DELETE);
        operationLogs.setOperationUser(CurrentUserCache.getId());
        this.save(operationLogs);

    }

    @Override
    public void recordingUpdate(String odlFilePath, String newFilePath) {
        OperationLogs operationLogs = new OperationLogs();
        operationLogs.setDes(CurrentUserCache.get().getName() + "修改" + odlFilePath + "为" + newFilePath);
        operationLogs.setType(OperationType.UPDATE);
        operationLogs.setOperationUser(CurrentUserCache.getId());
        this.save(operationLogs);
    }

    @Override
    public void recordingMove(String fromFilePath, String toFilePath) {
        OperationLogs operationLogs = new OperationLogs();
        operationLogs.setDes(CurrentUserCache.get().getName() + "将文件" + fromFilePath + "移动到" + toFilePath);
        operationLogs.setType(OperationType.UPDATE);
        operationLogs.setOperationUser(CurrentUserCache.getId());
        this.save(operationLogs);
    }

    @Override
    public void recordingDownload(String filePath) {
        OperationLogs operationLogs = new OperationLogs();
        operationLogs.setDes(CurrentUserCache.get().getName() + "下载了" + filePath);
        operationLogs.setType(OperationType.DOWNLOAD);
        operationLogs.setOperationUser(CurrentUserCache.getId());
        this.save(operationLogs);

    }
}
